﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace HCFramework.Product.ShoppingCart
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadGridMain.MasterTableView.NoMasterRecordsText = M.Common.GetLang(RadGridMain.MasterTableView.NoMasterRecordsText);
                foreach (GridColumn col in RadGridMain.Columns)
                {
                    col.HeaderText = M.Common.GetLang(col.HeaderText);
                }
            }
            this.Title = M.Common.GetLang(this.Title);
        }

        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            RadGridMain.DataSource = C.ShoppingCart.ListCartItem;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            //if (e.CommandName == "Xoa")
            //{
            //    GridDataItem item = (GridDataItem)e.Item;
            //    RoleController.Delete(Guid.Parse(item["RoleID"].Text));
            //}
        }

        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            if (RadGridMain.SelectedItems.Count < 1)
            {
                M.Message.ShowError("Chưa chọn sản phẩm để xóa");
                return;
            }
            foreach (GridItem gridItem in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)gridItem;
                int productId = int.Parse(dataItem["ProductId"].Text);

                C.ShoppingCart.Remove(productId);
            }
        }

        protected void LinkButtonUpdate_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGridMain.Items)
            {
                int productId = int.Parse(item["ProductId"].Text);
                TextBox txtCount = item.FindControl("txtCount") as TextBox;
                int count = int.Parse(txtCount.Text);
                C.ShoppingCart.Update(productId, count);
            }
        }

        protected void RadGridMain_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                int productId = int.Parse((e.Item as GridDataItem)["ProductId"].Text);
                TextBox txtCount = e.Item.FindControl("txtCount") as TextBox;
                Literal litPrice = e.Item.FindControl("litPrice") as Literal;
                Literal litTotalPrice = e.Item.FindControl("litTotalPrice") as Literal;
                litTotalPrice.Text = (int.Parse(txtCount.Text) * decimal.Parse(litPrice.Text)).ToString("N0");
                litPrice.Text = decimal.Parse(litPrice.Text).ToString("N0");

                HyperLink linkProductDetail = e.Item.FindControl("linkProductDetail") as HyperLink;
                linkProductDetail.NavigateUrl = "/Product/Detail/Default.aspx?productID=" + productId;
            }
            if (e.Item.ItemType == GridItemType.Footer)
            {
                Literal litTotalPrice = e.Item.FindControl("litTotalPrice") as Literal;
                litTotalPrice.Text = C.ShoppingCart.ListCartItem.Sum(ik => ik.Count * ik.Price).ToString("N0");
            }
        }

        protected void LinkButtonGoToPayment_Click(object sender, EventArgs e)
        {
            if (C.ShoppingCart.ListCartItem.Count < 1)
            {
                M.Message.ShowError("Giỏ hàng trống");
                return;
            }
            if (C.ShoppingCart.ListCartItem.Where(ik=>ik.Count < 1).Count() > 0)
            {
                M.Message.ShowError("Dữ liệu không hợp lệ");
                return;
            }
            Response.Redirect(@"~\Product\Payment\Default.aspx");
        }
    }
}